using System;
using System.Collections.Generic;
using Autofac.Core;
using Microsoft.Practices.ServiceLocation;

namespace Autofac.Integration.Prism
{
    /// <summary>
    /// Defines a <seealso cref="IContainer"/> adapter for the <see cref="IServiceLocator"/> interface to be used by the Composite Application Library.
    /// </summary>
	public class AutofacServiceLocatorAdapter: ServiceLocatorImplBase
	{
		readonly IContainer _container;

    	/// <summary>
    	/// Initializes a new instance of <see cref="AutofacServiceLocatorAdapter"/>.
    	/// </summary>
    	/// <param name="container">The <seealso cref="IContainer"/> that will be used
    	/// by the <see cref="DoGetInstance"/> and <see cref="DoGetAllInstances"/> methods.</param>
		public AutofacServiceLocatorAdapter(IContainer container)
		{
			_container = container;
		}

    	/// <summary>
    	/// Resolves the instance of the requested service.
    	/// </summary>
    	/// <param name="serviceType">Type of instance requested.</param>
    	/// <param name="key">Name of registered service you want. May be null.</param>
    	/// <returns>The requested service instance.</returns>
		protected override object DoGetInstance(Type serviceType, string key)
		{
			return string.IsNullOrEmpty(key) ? _container.Resolve(serviceType) : _container.ResolveNamed(key, serviceType);
		}

    	/// <summary>
    	/// Resolves all the instances of the requested service.
    	/// </summary>
    	/// <param name="serviceType">Type of service requested.</param>
    	/// <returns>Sequence of service instance objects.</returns>
    	protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
    	{
			throw new NotSupportedException();
		}
	}
}